package com.good.child.http;

import android.content.Context;
import android.text.TextUtils;

import com.good.child.R;
import com.good.child.bean.common.AppVersion;
import com.good.child.bean.constants.ConstantsBean;
import com.good.child.config.Api;
import com.good.child.config.RequestID;
import com.good.child.ui.utils.CommonU;
import com.lzy.okgo.model.Response;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.http.BaseReqBean;
import com.yimidida.library.http.LoadingBaseObserver;
import com.yimidida.library.tool.JsonSerializer;
import com.yimidida.library.utils.LogUtils;
import com.yimidida.library.utils.ToastUtil;

/**
 * Create on: 2024/8/4
 * by Mr.Zhu
 * Description: 通用的请求，更新等
 */
public class CommonLogical extends AppBaseLogical{
    private static CommonLogical instance;

    public static CommonLogical getInstance() {
        if (instance == null) {
            synchronized (CommonLogical.class) {
                if (instance == null) {
                    instance = new CommonLogical();
                }
            }
        }
        return instance;
    }



    public void requestUpdateApp(Context context, ICallBackResultListener resultListener) {
        String url = Api.CommonRequest.URL_UPDATE_APP;
        BaseReqBean reqBean = new BaseReqBean();
        requestStringForLogin(url, reqBean.getReqMap(), new CommonObserver(context, RequestID.Common.UPDATE_ID, resultListener));
    }


    public void requestUpdateLocation(Context context,String longitude,String latitude,ICallBackResultListener resultListener) {
        String url = Api.CommonRequest.URL_UPDATE_LOCATION;
        BaseReqBean reqBean = new BaseReqBean();
        reqBean.getMap().put("deviceCode", "1111");
        reqBean.getMap().put("longitude", longitude);
        reqBean.getMap().put("latitude", latitude);
        reqBean.getMap().put("updateTime", CommonU.date());
        requestStringForPostNew(url, reqBean.getReqMap(), new CommonObserver(null, RequestID.Login.LOGIN_ID, resultListener));
    }


    private class CommonObserver extends LoadingBaseObserver {
        private ICallBackResultListener resultListener;
        private int requestID;

        public CommonObserver(Context activity, int requestID, ICallBackResultListener resultListener) {
            this.resultListener = resultListener;
            this.requestID = requestID;
            setShowLoading(activity);
        }

        @Override
        public void onNext(Response<String> stringResponse) {
            super.onNext(stringResponse);
            if (stringResponse == null || stringResponse.code() != SUCCESS_CODE || TextUtils.isEmpty(stringResponse.body())) {
                ToastUtil.showLong(R.string.data_fail);
                return;
            }
            String bodyString = stringResponse.body();
            try {
                switch (requestID) {
                    case RequestID.Common.UPDATE_ID:
                        LogUtils.showLog("账号登录 ：" + bodyString);
                        AppVersion bean = JsonSerializer.deSerialize(bodyString, AppVersion.class);
                        if (bean.isDataSuccess()) {
                            if (resultListener != null) {
                                resultListener.onCallBack(bean.data);
                            }
                        } else {
                            ToastUtil.showLong(bean.msg);
                            if (resultListener != null) {
                                resultListener.onCallBack(null);
                            }
                        }
                        break;
                }
            } catch (Exception e) {
                ToastUtil.showLong(R.string.data_exception);
                if (resultListener != null) {
                    resultListener.onCallBack(0);
                }
            }

        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            ToastUtil.showLong(R.string.data_error);
            if (resultListener != null) {
                resultListener.onCallBack(null);
            }
        }
    }
}
